/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fandi.model;

import com.fandi.dao.ItemDao;
import com.fandi.dao.impl.ItemDaoImpl;
import com.fandi.entity.Item;
import com.fandi.listener.ItemListener;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Fandy
 */
public class ItemModel extends Item {
    private ItemListener listener;

    public ItemListener getListener() {
        return listener;
    }

    public void setListener(ItemListener listener) {
        this.listener = listener;
    }
    
    @Override
    public void setUid(int uid) {
        super.setUid(uid);
        fireOnChange();
    }

    @Override
    public void setCatId(int catId) {
        super.setCatId(catId);
        fireOnChange();
    }

    @Override
    public void setCode(String code) {
        super.setCode(code);
        fireOnChange();
    }

    @Override
    public void setName(String name) {
        super.setName(name);
        fireOnChange();
    }

    @Override
    public void setQty(int qty) {
        super.setQty(qty);
        fireOnChange();
    }

    @Override
    public void setExpired(Date expired) {
        super.setExpired(expired);
        fireOnChange();
    }

    @Override
    public void setDescription(String description) {
        super.setDescription(description);
        fireOnChange();
    }

    @Override
    public void setLastUpd(Date lastUpd) {
        super.setLastUpd(lastUpd);
        fireOnChange();
    }
    
    public void fireOnChange() {
        if (listener != null) {
            listener.onChange(this);
        }
    }
    
    public void insert() throws SQLException {
        ItemModel model = new ItemModel();
        model.setCatId(getCatId());
        model.setCode(getCode());
        model.setName(getName());
        model.setQty(getQty());
        model.setExpired(getExpired());
        model.setDescription(getDescription());
        model.setLastUpd(getLastUpd());
        
        ItemDao dao = new ItemDaoImpl();
        dao.insert(model);
    }
    
    public void update(String code) throws SQLException {
        ItemDao dao = new ItemDaoImpl();
        ItemModel model = dao.selectBy(code);
        
        model.setUid(model.getUid());
        model.setCatId(getCatId());
        model.setCode(getCode());
        model.setName(getName());
        model.setQty(getQty());
        model.setExpired(getExpired());
        model.setDescription(getDescription());
        model.setLastUpd(getLastUpd());
        
        dao.update(model);
    }
    
    public List<ItemModel> selectAll() throws SQLException {
        ItemDao dao = new ItemDaoImpl();
        List<ItemModel> list = dao.selectAll();
        
        return list;
    }
    
    public ItemModel selectById(String code) throws SQLException {
        ItemDao dao = new ItemDaoImpl();
        ItemModel model = dao.selectBy(code);
        
        return model;
    }
    
    public boolean delete(String code) throws SQLException {
        ItemDao dao = new ItemDaoImpl();
        
        return dao.delete(code);
    }
    
    public List<ItemModel> searchBy(String code) throws SQLException {
        ItemDao dao = new ItemDaoImpl();
        List<ItemModel> list = dao.searchBy(code);
        
        return list;
    }
    
    public int count() throws SQLException {
        ItemDao dao = new ItemDaoImpl();
        int count = dao.count(false);
        
        return count;
    }
    
    public int countExpired() throws SQLException {
        ItemDao dao = new ItemDaoImpl();
        int count = dao.count(true);
        
        return count;
    }
    
    public void reset() {
        setCode("");
        setCatId(0);
        setName("");
        setExpired(null);
        setLastUpd(null);
        setQty(0);
        setDescription("");
    }
}
